#!ipxe

# Talos
# https://github.com/siderolabs/talos/releases
# https://www.talos.dev/v1.6/talos-guides/install/bare-metal-platforms/pxe

goto ${menu} ||

:talos
set os {{ releases.talos.name }}
set os_arch ${arch}
iseq ${os_arch} x86_64 && set os_arch amd64 ||
iseq ${os_arch} arm64 && set os_arch arm64 ||
isset ${talos_version} || set talos_version latest
isset ${talos_mirror} || set talos_mirror {{ releases.talos.mirror }}
isset ${talos_platform} || set talos_platform metal
menu ${os} by Talos Systems
menu ${os} install
item --gap Talos:
item talos_boot ${space} Begin install ${os} ${talos_version}
item --gap Parameters:
item talos_version ${space} ${os} version: ${talos_version}
item talos_config_url ${space} Set userdata.yaml URL: ${talos_config_url}
item talos_mirror ${space} Set mirror URL: ${talos_mirror}
item talos_platform ${space} Set platform: ${talos_platform}
choose --default ${menu} menu || goto talos_exit
echo ${cls}
goto ${menu} ||
goto talos_exit

:talos_version
menu ${os} version
item latest ${space} latest
item custom ${space} Set custom version
choose --default ${version} version || goto talos_exit
echo ${cls}
goto talos_version_${version} ||
goto talos_exit

:talos_version_latest
set talos_version latest
set talos_base_url ${talos_mirror}/latest/download
goto talos

:talos_version_custom
clear talos_version
echo -n Please set Talos version manually (in format vX.Y.Z):  && read talos_version
set talos_base_url ${talos_mirror}/download/${talos_version}
clear menu
goto talos

:talos_mirror
echo -n Set mirror URL:  && read talos_mirror
clear menu
goto talos

:talos_config_url
echo -n Set userdata.yaml URL:  && read talos_config_url
clear menu
goto talos

# https://www.talos.dev/latest/reference/kernel/#talosplatform
:talos_platform
menu ${os} platforms
{% for item in releases.talos.platforms %}
item {{ item.key }} ${space} {{ item.name }}
{% endfor %}
choose --default ${talos_platform} talos_platform
goto talos

# https://www.talos.dev/latest/reference/kernel/
:talos_boot
isset ${talos_base_url} || set talos_base_url ${talos_mirror}/latest/download
isset ${talos_config_url} && set talos_config talos.config=${talos_config_url} ||
{# Edit releases.talos.custom_kernel_params in main.yml for custom kernel params #}
{% if releases.talos.custom_kernel_params is defined and releases.talos.custom_kernel_params %}
set boot_params {{ releases.talos.custom_kernel_params }}
{% else %}
set boot_params printk.devkmsg=on slab_nomerge pti=on console=ttyS0 console=tty0 init_on_alloc=1 init_on_free=1 consoleblank=0 nvme_core.io_timeout=4294967295 ima_template=ima-ng ima_appraise=fix ima_hash=sha512 talos.platform=${talos_platform} ${talos_config} initrd=initrd.magic ${cmdline}
{% endif %}
imgfree
kernel ${talos_base_url}/vmlinuz-${os_arch} ${boot_params}
initrd ${talos_base_url}/initramfs-${os_arch}.xz
echo
echo Booting with the following kernel args:
echo ${boot_params}
echo
echo MD5sums:
md5sum vmlinuz-${os_arch} initramfs-${os_arch}.xz
boot

:talos_exit
clear menu
exit 0
